package com.credithc.signature.manager.service;

import com.credithc.baseserv.pageHelper.PageInfo;
import com.credithc.signature.common.mapper.ChannelMapper;
import com.credithc.signature.common.po.Channel;
import com.credithc.signature.manager.ro.ChannelRo;
import com.credithc.signature.manager.ro.ProductRo;
import com.credithc.signature.manager.vo.ChannelVo;
import com.credithc.signature.manager.vo.CompanyProductVo;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ChannelMService {

    @Autowired
    private CompanyService companyService;

    @Autowired
    private ChannelMapper channelMapper;

    public PageInfo<ChannelVo> queryAll(ChannelRo channelRo) {


        Integer companyId = channelRo.getCompanyId();
        String code = channelRo.getCode();
        CompanyProductVo productMap = companyService.getProductMap(code,companyId, null);
        List<String> channelIds = productMap.getChannelIds();
        Map<String, ProductRo> proMap = productMap.getProMap();
        if (  companyId !=null){
            if ( channelIds.size()<1){
                return new PageInfo<>(new ArrayList<>(),0,0,0);
            }else  {
                channelRo.setChannelIds(channelIds);
            }
        }
        Example e = new Example(Channel.class);
        Example.Criteria criteria = e.createCriteria();
        e.setOrderByClause("create_time desc");
        if (StringUtils.isNotBlank(channelRo.getTimeRange())){
            criteria.andBetween("createTime",channelRo.getCreateTimeS(),channelRo.getCreateTimeE());
        }
        if (StringUtils.isNotBlank(channelRo.getCode())){
            criteria.andEqualTo("channelNo",channelRo.getCode());
        }
        if (channelRo.getChannelIds() != null && channelRo.getChannelIds().size()>0){
            criteria.andIn("channelNo",channelIds);
        }
        List<Channel> channels = null;
        int i = 0;
        try {
            channels = channelMapper.selectByExampleAndRowBounds(e, new RowBounds((channelRo.getPageNo() - 1) * channelRo.getPageSize(), channelRo.getPageSize()));
            i = channelMapper.selectCountByExample(e);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        List<ChannelVo> channelVos = new ArrayList<>();
        for (Channel c:channels) {
            ChannelVo channelVo = new ChannelVo();
            BeanUtils.copyProperties(c,channelVo);
            try {
                channelVo.setTenant(proMap.get(c.getChannelNo()).getCompanyName());
                channelVo.setProductCode(proMap.get(c.getChannelNo()).getProductCode());
            } catch (Exception e1) {
            }
            channelVos.add(channelVo);
        }

        return new PageInfo<>(channelVos,i,channelRo.getPageNo(),channelRo.getPageSize());
    }
}
